package com.hospital.pharmacy.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.dao.InventoryMapper;
import com.hospital.pharmacy.dao.MedicineMapper;
import com.hospital.pharmacy.dao.OverdueRecordMapper;
import com.hospital.pharmacy.entity.Inventory;
import com.hospital.pharmacy.entity.Medicine;
import com.hospital.pharmacy.entity.OverdueRecord;
import com.hospital.pharmacy.service.OverdueRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: Hospital
 * @description: 过期实现
 * @author: 杨曼珍
 * @create: 2019-01-07 11:30
 **/
@Service
public class OverdueRecordServiceImpl implements OverdueRecordService {
    @Autowired
    OverdueRecordMapper overdueRecordMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    InventoryMapper inventoryMapper;


    public Map getAll(OverdueRecord overdueRecord){
        Map<String,String>map;
        map=new HashMap<>();
        map.put("id",overdueRecord.getId().toString());
        int med_id=overdueRecordMapper.selectByPrimaryKey(overdueRecord.getId()).getOvdRecMdcId();
        Medicine medicine=medicineMapper.selectByPrimaryKey(med_id);
        map.put("mdcName",medicine.getMdcName());
        map.put("mdcUnit",medicine.getMdcUnit());
        map.put("mdcSpec",medicine.getMdcSpec());
        map.put("ovdRecCount",overdueRecord.getOvdRecCount().toString());
        map.put("ovdRecDate",new SimpleDateFormat("yyyy-MM-dd").format(overdueRecord.getOvdRecDate()));
        map.put("ovdRecPerson",overdueRecord.getOvdRecPerson());
        map.put("ovdRecIllustration",overdueRecord.getOvdRecIllustration());
        map.put("ovdRecRemark",overdueRecord.getOvdRecRemark());

         return map;
    }

    @Override
    public String getAllOver(){
        List<OverdueRecord>overdueRecords=overdueRecordMapper.selectAllOver();
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (OverdueRecord overdueRecord:overdueRecords){
            resaultmap.add(getAll(overdueRecord));
        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public  String getOverByMedName(String med_name){
        List <Medicine>medicines=medicineMapper.selectByMdcName(med_name);
        List<Map<String, String>> resaultmap = new ArrayList<>();
        for (Medicine medicine:medicines) {
            List<OverdueRecord>overdueRecords=overdueRecordMapper.selectOverByMedId(medicine.getId());
            for (OverdueRecord overdueRecord:overdueRecords){
                resaultmap.add(getAll(overdueRecord));
            }
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public  String getOverByDate(Date date){
        List<OverdueRecord>overdueRecords=overdueRecordMapper.selectOverByDate(date);
        List<Map<String, String>> resaultmap = new ArrayList<>();
        for (OverdueRecord overdueRecord:overdueRecords){
                resaultmap.add(getAll(overdueRecord));
            }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public  String getOverByPerson(String name){
        List<OverdueRecord>overdueRecords=overdueRecordMapper.selectOverByPerson(name);
        List<Map<String, String>> resaultmap = new ArrayList<>();
        for (OverdueRecord overdueRecord:overdueRecords){
            resaultmap.add(getAll(overdueRecord));
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public  String add(int meddId,int ovdRecCount,Date ovdRecDate,String ovdRecIllustration,String ovdRecPerson,String ovdRemark){

        OverdueRecord overdueRecord=new OverdueRecord();
        overdueRecord.setOvdRecCount(ovdRecCount);
        overdueRecord.setOvdRecDate(ovdRecDate);
        overdueRecord.setOvdRecIllustration(ovdRecIllustration);
        overdueRecord.setOvdRecPerson(ovdRecPerson);
        overdueRecord.setOvdRecRemark(ovdRemark);
        overdueRecord.setOvdRecMdcId(meddId);
        overdueRecordMapper.insert(overdueRecord);
        inventoryMapper.selectByMdcId(meddId).setIvtrCount(inventoryMapper.selectByMdcId(meddId).getIvtrCount()-ovdRecCount);

        return getAllOver();
    }
    @Override
    public  String update(int id,int ovdRecCount,Date ovdRecDate,String ovdRecIllustration,String ovdRemark){
        OverdueRecord overdueRecord=overdueRecordMapper.selectByPrimaryKey(id);
        Inventory inventory=inventoryMapper.selectByMdcId(overdueRecord.getOvdRecMdcId());
        if (overdueRecord.getOvdRecCount()-ovdRecCount<0){
            inventory.setIvtrCount(inventory.getIvtrCount()-(ovdRecCount-overdueRecord.getOvdRecCount()));
        }else {
            inventory.setIvtrCount(inventory.getIvtrCount()+(overdueRecord.getOvdRecCount()-ovdRecCount));
        }
        overdueRecord.setOvdRecCount(ovdRecCount);
        overdueRecord.setOvdRecDate(ovdRecDate);
        overdueRecord.setOvdRecIllustration(ovdRecIllustration);
        overdueRecord.setOvdRecRemark(ovdRemark);
        overdueRecordMapper.updateByPrimaryKey(overdueRecord);
        inventoryMapper.updateByPrimaryKey(inventory);
        return getAllOver();
    }
    @Override
    public  String delete(int id){
        Inventory inventory=inventoryMapper.selectByMdcId(overdueRecordMapper.selectByPrimaryKey(id).getOvdRecMdcId());
        inventory.setIvtrCount(inventory.getIvtrCount()+overdueRecordMapper.selectByPrimaryKey(id).getOvdRecCount());
        inventoryMapper.updateByPrimaryKey(inventory);
        overdueRecordMapper.deleteByPrimaryKey(id);
        return getAllOver();
    }






}
